package com.hfzy.ihk.common.support.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.support.Acknowledgment;

import java.util.Optional;

/**
 * Created by Administrator on 2018/3/1.
 *
 * @author wws
 */
public abstract class AbstractKafkaMessageReceiver {

    private static final Logger logger = LoggerFactory.getLogger(AbstractKafkaMessageReceiver.class);

    protected void dealMessage(ConsumerRecord<String, String> data, Acknowledgment ack, MessageDealer dealer) {

        Optional<String> kafkaMessage = Optional.ofNullable(data.value());

        if (!kafkaMessage.isPresent()) {

            ack.acknowledge();
            return;
        }

        String message = kafkaMessage.get();

        if(dealer.toDeal(message)) {
            ack.acknowledge();
        }
    }
}
